
/**
 * @author LKQ
 * @date 2021/12/5 11:26
 * @description DFS
 */
public class Solution {
    public static void main(String[] args) {
        TreeNode leaf1 = new TreeNode(1, null, null);
        TreeNode leaf2 = new TreeNode(1, null, null);
        TreeNode root = new TreeNode(1, null, leaf1);
        TreeNode root2 = new TreeNode(1, leaf2, null);
        Solution solution = new Solution();
        System.out.println(solution.isSameTree(root, root2));
    }

    public boolean isSameTree(TreeNode p, TreeNode q) {
        if (p == null && q == null) {
            // 两个节点同时为null, 返回true
            return true;
        } else if (p == null || q == null) {
            // 只有一个为null, 另一个不为null, 肯定不相同，返回false
            return false;
        } else if (p.val != q.val) {
            // 都不为null, 看值是否相等，不相等返回 false
            return false;
        } else {
            // 根节点看完了，比较左子树 && 右子树是否相等
            return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
        }
    }
}
